<?php
namespace zhtfrontend\controllers;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/11
 * Time: 9:36
 */
use common\models\datas\Customer;
use common\models\datas\Student;
use doumiaoduo\buildform\BuildForm;
use Yii;
use yii\helpers\Json;
use yii\rest\ViewAction;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\data\Pagination;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use yii\web\BadRequestHttpException;
use common\components\actions\DeleteAction;
use moonland\phpexcel\Excel;

class StudentController extends \zhtfrontend\components\bases\BaseController
{
    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
            $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
//            $data = Student::find()->where(['site_id'=>Fun::getSiteId()])->orderBy('id ASC');
            $data = Student::find()
                ->from('{{%student}} s')
                ->leftJoin('{{%customer}} c','s.customer_id = c.id')
                ->leftJoin('{{%customer_register_relation}} crr','crr.customer_id = c.id')
                ->where(['crr.site_id'=>Fun::getSiteId()])
                ->orderBy('s.id DESC');
            if (!empty($param['searchData'])) {
                $search_data = array_filter($param['searchData']);
                if(!empty($search_data['year']) && !empty($search_data['mouth'])){
                    if($search_data['types'] == 2) {
                        $time = $search_data['year'] . "-" . $search_data['mouth'];
                        $end = Fun::getCurMonthLastDay($time);
                        $data->andWhere(['<=', 's.created_at', $end]);
                    }
                    else{
                        $time = $search_data['year'] . "-" . $search_data['mouth'];
                        $start = Fun::getCurMonthFirstDay($time);
                        $end = Fun::getCurMonthLastDay($time);
                        $data->andWhere(['between', 's.created_at', $start, $end]);
                    }
                }
//                else{
//                    $timestamp=strtotime('-1 month');
//                    $time = date('Y-m-01',$timestamp);
//                    //$start = Fun::getCurMonthFirstDay($time);
//                    $end = Fun::getCurMonthLastDay($time);
//                    $data->andWhere(['<=', 's.created_at', $end]);
//                }
                if (!empty($search_data['name'])) {
                    $data->andWhere(['like', 's.name', $search_data['name']]);
                }
            }
            $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
            isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
            $model = $data->offset($pages->offset)->limit($pages->limit)->all();
            $array = Fun::asArray($model,['common_operate','customer_name']);

        return $this->render('index',['data'=>$array,'pages'=>$pages]);
    }

    public function actionUpdate()
    {
        $this->layout='@app/views/layouts/iframe.php';
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '错误请求'));
        }
        $model = Student::findOne(\Yii::$app->request->get('id'));
        $model->scenario='update';
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $form = new BuildForm('@app/components/forms/studentForm.php');
        $form->loadModel(['student' => $model]);
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '信息修改成功'), Fun::url(['student/index']));
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actions(){
        return [
            'delete' => [
                'class' => DeleteAction::className(),
                'modelClass' => Student::className(),
                'message' => ['success'=>'学员删除成功！'],
                'redirectTo' => 'student/index',
            ],
        ];
    }
}